<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Page {
    /**
     * 本分页类专为三段式CI分页缩写，即site_url('control/function/2');
     */

    /**
     * config
     */
    public $pageRuler = ""; //页面规则
    public $part = 2; //控制数字列表当前页前后链接数量
    public $totalpage = 0; //总页数
    public $url = ''; //url地址,不含分页所在的段,形如：'control/function'
    public $total = 0; //总条数
    public $perpage = 5; //每页条数
    public $nowindex = 1; //当前页
    public $seg = 3; //页码参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
    public $next_page = '下一页'; //下一页
    public $pre_page = '上一页'; //上一页
    public $first_page = '首页'; //首页
    public $last_page = '末页'; //尾页
    # 新增分页各链接样式 add by yh 2012-10-16
    public $first_page_class = "first_page"; // 首页样式
    public $pre_page_class = "pre_page"; // 上一页样式
    public $next_page_class = "next_page"; // 下一页样式
    public $last_page_class = "last_page"; // 尾页样式
    public $nowbar_curr_page_class = "pg_curr"; // 循环 当前选中样式
    public $nowbar_not_curr_page_class = "pg_link"; // 循环 当前未选中样式
    public $nowbar_curr_tag = "b"; // 循环 当前选中标签
    public $link_tag_open = ""; //连接页的打开标签
    public $link_tag_close = ""; //连接页的关闭标签
    public $pg_linkto = ''; //锚点

    /**
     * constructor构造函数
     *
     * @param $params=array()
     */

    public function __construct($params = array()) {
        if (count($params) > 0) {
            $this->initialize($params);
        }
        log_message('debug', "MyPage Class Initialized");
    }

    function initialize($params) {
        if (count($params) > 0) {

            $this->pageRuler = isset($params['pageRuler']) ? $params['pageRuler'] : ''; //页面规则

            $this->total = isset($params['total']) ? intval($params['total']) : 0; //总条数
            $this->perpage = isset($params['perpage']) ? intval($params['perpage']) : 5; //每页条数
            $this->nowindex = isset($params['nowindex']) ? intval($params['nowindex']) : 1; //当前页
            $this->url = isset($params['url']) ? $params['url'] : ''; //url地址,不含分页所在的段,形如：'control/function'
            $this->part = isset($params['part']) ? $params['part'] : 2; //控制数字列表当前页前后链接数量
            $this->seg = isset($params['seg']) ? $params['seg'] : FALSE; //页码参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
            $this->next_page = isset($params['next_page']) ? $params['next_page'] : '上一页';
            $this->pre_page = isset($params['pre_page']) ? $params['pre_page'] : '下一页';
            $this->first_page = isset($params['first_page']) ? $params['first_page'] : '首页';
            $this->last_page = isset($params['last_page']) ? $params['last_page'] : '尾页';

            $this->pg_linkto = isset($params['pg_linkto']) ? $params['pg_linkto'] : '';

            $this->first_page_class = isset($params['first_page_class']) ? $params['first_page_class'] : 'first_page';
            $this->pre_page_class = isset($params['pre_page_class']) ? $params['pre_page_class'] : 'pre_page';
            $this->next_page_class = isset($params['next_page_class']) ? $params['next_page_class'] : 'next_page';
            $this->last_page_class = isset($params['last_page_class']) ? $params['last_page_class'] : 'last_page';
            $this->nowbar_curr_page_class = isset($params['nowbar_curr_page_class']) ? $params['nowbar_curr_page_class'] : 'pg_curr';
            $this->nowbar_not_curr_page_class = isset($params['nowbar_not_curr_page_class']) ? $params['nowbar_not_curr_page_class'] : 'pg_link';
            $this->nowbar_curr_tag = isset($params['nowbar_curr_tag']) ? $params['nowbar_curr_tag'] : 'b';

            $this->link_tag_open = isset($params['link_tag_open']) ? $params['link_tag_open'] : '';
            $this->link_tag_close = isset($params['link_tag_close']) ? $params['link_tag_close'] : '';
        }
        $this->totalpage = ceil($this->total / $this->perpage); //总页数
        $this->_myset_url($this->url, $this->seg); //设置链接地址
    }

    /**
     * 获取显示"下一页"的代码
     * 
     * @param string $style
     * @return string
     */
    function next_page($style = 'next_page') {
        if ($this->nowindex < $this->totalpage) {
            return $this->_get_link($this->_get_url($this->nowindex + 1), $this->next_page, $style);
        }
        return $this->link_tag_open . '<a class="' . $this->nowbar_curr_page_class . '">' . $this->next_page . '</a>' . $this->link_tag_close;
    }

    /**
     * 获取显示上一页的代码
     *
     * @param string $style
     * @return string
     */
    function pre_page($style = 'next_page') {
        if ($this->nowindex > 1) {
            return $this->_get_link($this->_get_url($this->nowindex - 1), $this->pre_page, $style);
        }
        return $this->link_tag_open . '<a class="' . $this->nowbar_curr_page_class . '">' . $this->pre_page . '</a>' . $this->link_tag_close;
    }

    /**
     * 获取显示&ldquo;首页&rdquo;的代码
     *
     * @param string $style
     * @return string
     */
    function first_page($style = 'first_page') {
        if ($this->nowindex == 1) {
            return $this->link_tag_open . '<a class="' . $this->nowbar_curr_page_class . '">' . $this->first_page . '</a>' . $this->link_tag_close;
        }
        return $this->_get_link($this->_get_url(1), $this->first_page, $style);
    }

    /**
     * 获取显示&ldquo;尾页&rdquo;的代码
     *
     * @param string $style
     * @return string
     */
    function last_page($style = 'next_page') {
        if ($this->nowindex == $this->totalpage) {
            return $this->link_tag_open . '<a class="' . $this->nowbar_curr_page_class . '">' . $this->last_page . '</a>' . $this->link_tag_close;
        }
        return $this->_get_link($this->_get_url($this->totalpage), $this->last_page, $style);
    }

    /**
     * 获取显示当前页的代码
     *
     * @param string $style
     * @param string $nowindex_style
     * @return string
     */
    function nowbar($style = 'pg_link', $nowindex_style = 'pg_curr', $now_tag = "a") {
        $plus = $this->part;
        $begin = 1;
        $end = $this->totalpage;

        if ($this->nowindex > $plus) {
            $begin = $this->nowindex - $plus;
            $end = $this->nowindex + $plus;
            if ($end > $this->totalpage) {
                $begin = ($begin - $end + $this->totalpage > 0) ? ($begin - $end + $this->totalpage) : 1;
                $end = $this->totalpage;
            }
        } else {
            $begin = 1;
            $end = $begin + 2 * $plus;
            $end = $end > $this->totalpage ? $this->totalpage : $end;
        }
        $out = '';
        for ($i = $begin; $i <= $end; $i++) {
            if ($i != $this->nowindex) {
                $out.= $this->_get_link($this->_get_url($i), $i, $style);
            } else {
                $out.= $this->link_tag_open . '<' . $now_tag . ' class="' . $nowindex_style . '">' . $i . '</' . $now_tag . '>' . $this->link_tag_close;
            }
        }

        return $out;
    }

    /**
     * 获取显示跳转按钮的代码
     *
     * @return string
     */
    function select() {
        $out = '<select name="pagelect" class="pg_select">';
        for ($i = 1; $i <= $this->totalpage; $i++) {
            if ($i == $this->nowindex) {
                $out.='<option value="' . $i . '" selected>' . $i . '</option>';
            } else {
                $out.='<option value="' . $i . '">' . $i . '</option>';
            }
        }
        $out.='</select>';
        return $out;
    }

    /**
     * 控制分页显示风格
     * 
     * @param int $mode
     * @return string
     */
    function show($mode = 1) {
        switch ($mode) {
            case 1://上一页 1 2 3 4 5 下一页 第x页
                return $this->pre_page($this->pre_page_class) . $this->nowbar($this->nowbar_not_curr_page_class, $this->nowbar_curr_page_class, $this->nowbar_curr_tag) . $this->next_page($this->next_page_class);
                break;
            case 2://首页 上一页 1 2 3 4 5 下一页  末页 第x页
                return $this->first_page($this->first_page_class) . $this->pre_page($this->pre_page_class) . $this->nowbar($this->nowbar_not_curr_page_class, $this->nowbar_curr_page_class, $this->nowbar_curr_tag) . $this->next_page($this->next_page_class) . $this->last_page($this->last_page_class) . '&nbsp;第' . $this->select() . '页';
                break;
            case 3://上一页 1 2 3 4 5 下一页 
                return $this->pre_page($this->pre_page_class) . $this->nowbar($this->nowbar_not_curr_page_class, $this->nowbar_curr_page_class, $this->nowbar_curr_tag) . $this->next_page($this->next_page_class);
                break;
            case 4://首页 上一页 1 2 3 4 5 下一页  末页 
                return $this->first_page($this->first_page_class) . $this->pre_page($this->pre_page_class) . $this->nowbar($this->nowbar_not_curr_page_class, $this->nowbar_curr_page_class, $this->nowbar_curr_tag) . $this->next_page($this->next_page_class) . $this->last_page($this->last_page_class);
                break;
            case 5://首页 上一页  下一页  末页 
                return $this->first_page($this->first_page_class) . $this->pre_page($this->pre_page_class) . $this->next_page($this->next_page_class) . $this->last_page($this->last_page_class);
                break;
            default://上一页 1 2 3 4 5 下一页  第x页
                return $this->pre_page($this->pre_page_class) . $this->nowbar($this->nowbar_not_curr_page_class, $this->nowbar_curr_page_class, $this->nowbar_curr_tag) . $this->next_page($this->next_page_class) . '&nbsp;第' . $this->select() . '页';
                break;
        }
    }

    /* ----------------private function (私有方法)----------------------------------------------------------- */

    /**
     * 设置url头地址
     * @param: String $url
     * @return boolean
     */
    public function _myset_url($url) {
        $CI = &get_instance();
        $CI->load->helper('url');
        if (empty($url)) {//如果$url为空，要用uri_string()函数取uri段
            $cururl = '';
            $cururl = uri_string();
            $segementarray = explode("/", $cururl);
            $c = 0;
            for ($i = 0; $i < sizeof($segementarray); $i++) {
                if ($segementarray[$i] && $c < $this->seg - 1) {//取uri_string()的seg-1段
                    $url = $url . $segementarray[$i];
                    $c++;
                }
            }
        }

        //$this->url=site_url($url);
        $this->url = $url;
    }

    /**
     * 为指定的页面返回地址值
     *
     * @param int $pagenum
     * @param: $segType 类型
     * @return string $url
     */
    function _get_url($pagenum = 1, $segType = '') {
        if (strpos($this->url, "%s") === false) {
            $segType = $this->seg;
            if (!$segType) {
                return $this->url . $pagenum;
            } else {
                return $this->url . '/' . $pagenum;
            }
        } else {
            if ($pagenum) {
                if ($this->pageRuler) {
                    return sprintf($this->pageRuler, $pagenum);
                }
            }
        }
    }

    /**
     * 获取链接地址
     */
    function _get_link($url, $text, $style = '') {
        $style = $style ? 'class="' . $style . '"' : '';
        return $this->link_tag_open . '<a ' . $style . ' href="' . $url . $this->pg_linkto . '">' . $text . '</a>' . $this->link_tag_close;
    }

}

//end class
/* End of file Mypage.php */
/* Location: ./application/libraries/Mypage.php */